package com.ctshk.rpc.air.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.AirTouristType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.utils.Assert;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.air.dto.AirProductCostDTO;
import com.ctshk.rpc.air.dto.AirProductCostTaxationDTO;
import com.ctshk.rpc.air.dto.AirProductCostTouristDTO;
import com.ctshk.rpc.air.dto.AirTouristTypeDTO;
import com.ctshk.rpc.air.entity.AirProductCost;
import com.ctshk.rpc.air.entity.AirProductCostTaxation;
import com.ctshk.rpc.air.entity.AirProductCostTourist;
import com.ctshk.rpc.air.mapper.AirProductCostMapper;
import com.ctshk.rpc.air.mapper.AirProductCostTaxationMapper;
import com.ctshk.rpc.air.mapper.AirProductCostTouristMapper;
import com.ctshk.rpc.air.service.IAirProductCostTouristService;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 机票产品成本-旅客成本表 服务实现类
 * </p>
 *
 * @author lihang
 * @since 2021-03-05
 */
@DubboService
public class AirProductCostTouristServiceImpl extends ServiceImpl<AirProductCostTouristMapper, AirProductCostTourist> implements IAirProductCostTouristService {
    @Resource
    private AirProductCostMapper airProductCostMapper;
    @Resource
    private AirProductCostTaxationMapper airProductCostTaxationMapper;

    @Override
    public Result<List<AirTouristTypeDTO>> queryTouristType() {
        List<AirTouristTypeDTO> airTouristTypeDTOList = Lists.newArrayList();
        for (AirTouristType value : AirTouristType.values()) {
            AirTouristTypeDTO airTouristTypeDTO = new AirTouristTypeDTO();
            airTouristTypeDTO.setTouristType(value.getType());
            airTouristTypeDTO.setTouristTypeName(value.getName());
            airTouristTypeDTOList.add(airTouristTypeDTO);

        }
        return Result.success(airTouristTypeDTOList);
    }

    @Override
    public AirProductCostDTO selectByProductIdAndTouristTypes(Long productId, List<Integer> touristTypes) {
        List<AirProductCost> list = airProductCostMapper.selectValidDateCost(productId, LocalDate.now());
        if (CollectionUtils.isNotEmpty(list)) {
            AirProductCost cost = list.get(0);
            //获取对应旅客类型的成本
            List<AirProductCostTourist> touristList = baseMapper.selectList(Wrappers.<AirProductCostTourist>lambdaQuery()
                    .eq(AirProductCostTourist::getCostId, cost.getId())
                    .in(AirProductCostTourist::getTouristTypeId, touristTypes)
            );
            List<AirProductCostTouristDTO> copyList = EntityUtil.copyList(touristList, AirProductCostTouristDTO.class);
            copyList.stream().forEach(item -> {
                List<AirProductCostTaxation> taxationList = airProductCostTaxationMapper.selectList(Wrappers.<AirProductCostTaxation>lambdaQuery()
                        .eq(AirProductCostTaxation::getTouristId, item.getId())
                        .eq(AirProductCostTaxation::getProductId, productId)
                );
                if (CollectionUtils.isNotEmpty(taxationList)) {
                    item.setAirProductCostTaxationDTOList(EntityUtil.copyList(taxationList, AirProductCostTaxationDTO.class));
                    item.setTaxation(taxationList.stream().map(AirProductCostTaxation::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
                }
            });
            AirProductCostDTO copy = EntityUtil.copy(cost, AirProductCostDTO.class);
            copy.setAirProductCostTouristDTOList(copyList);
            return copy;
        }
        return null;
    }

    @Override
    public List<AirProductCostTouristDTO> getCurrentlyValidData(Long productId, LocalDate date) {
        //获取当前可用成本
        AirProductCost cost = airProductCostMapper.getCurrentlyValidCost(productId,date);
        Assert.nonNull(cost, SystemError.AIR_PRODUCT_12025);
        //获取旅客成本
        List<AirProductCostTourist> list = list(Wrappers.<AirProductCostTourist>lambdaQuery().eq(AirProductCostTourist::getCostId, cost.getId()));
        List<AirProductCostTouristDTO> dtos = EntityUtil.copyList(list, AirProductCostTouristDTO.class);
        //获取税费信息
        dtos.stream().forEach(item->{
            List<AirProductCostTaxation> airProductCostTaxations = airProductCostTaxationMapper.selectList(Wrappers.
                    <AirProductCostTaxation>lambdaQuery().eq(AirProductCostTaxation::getTouristId, item.getId()));
            item.setAirProductCostTaxationDTOList(EntityUtil.copyList(airProductCostTaxations,AirProductCostTaxationDTO.class));
        });
        return dtos;
    }

}
